package com.hyj.app.demo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ProcessObserver {

    private RecyclerView recyclerView;
    private ProcessManagerService processService;
    private List<AppInfo> appList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 模拟JSON数据加载
        appList.add(new AppInfo("a1", "A1a1a1啦啦啦啦"));
        appList.add(new AppInfo("a2", "A2a2a2哈哈哈啊哈"));

        // 绑定服务
        bindService(new Intent(this, ProcessManagerService.class), serviceConn, BIND_AUTO_CREATE);
        initRecyclerView();
    }

    private void initRecyclerView() {
        recyclerView = findViewById(R.id.rv_apps);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new AppAdapter(appList, this::launchSubProcess));
    }

    // 启动子进程逻辑
    private void launchSubProcess(AppInfo app) {
        if (processService != null && processService.isProcessRunning(app.getAppName())) {
            // 复用已有进程
            Intent intent = new Intent(this, SubProcessActivity.class);
            intent.putExtra("text", app.getText());
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        } else {
            // 创建新进程
            Intent intent = new Intent(this, SubProcessActivity.class);
            intent.putExtra("text", app.getText());
            intent.putExtra("process_name", app.getAppName());
            intent.putExtra("app_name", app.getAppName());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            startActivity(intent);
        }
    }

    // 进程状态回调
    @Override
    public void onProcessCreated(String processName) {
        Log.d("Process", "Created: ============== " + processName);
    }

    @Override
    public void onProcessDestroyed(String processName) {
        Log.d("Process", "Destroyed: " + processName);
    }

    private final ServiceConnection serviceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            processService = ((ProcessManagerService.LocalBinder) service).getService();
            processService.registerObserver(MainActivity.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            processService = null;
        }
    };

    @Override
    public IBinder asBinder() {
        return processService.onBind(new Intent());
    }
}
